﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace NetWorkSet
{
    public partial class GaiMing : Form
    {
        string tabName = null;
        int WorkStyle;
        public GaiMing(string Name, string WorkName, int style)
        {
            InitializeComponent();
            this.Text = WorkName;
            tabName = Name;
            WorkStyle = style;//操作类型；修改或者新增
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("NETWORKSET.xml");

            //修改操作
            if (WorkStyle == 1)
            {
                //Form1 f1 = new Form1();
                //f1.SetTabName(this.textBox1.Text.Trim());

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("NETWORKSET").ChildNodes;//获取bookstore节点的所有子节点
                foreach (XmlNode xn in nodeList)//遍历所有子节点
                {
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型


                    if (xe.GetAttribute("TEXT") == tabName)//
                    {
                        xe.SetAttribute("TEXT", this.textBox1.Text.Trim());
                        XmlNodeList nls = xe.ChildNodes;
                        break;
                    }
                }
                xmlDoc.Save("NETWORKSET.xml");//保存。
                this.Close();
            }
            else if (WorkStyle == 2)
            {
                DataSet ds = new DataSet();
                ds.ReadXml("NETWORKSET.xml");
                string[] TabelName=new string[ds.Tables.Count];
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    TabelName[i] = ds.Tables[i].TableName.Substring(3,1);
                }



                XmlNodeList nodeList = xmlDoc.SelectSingleNode("NETWORKSET").ChildNodes;//获取bookstore节点的所有子节点
                XmlNode root = xmlDoc.SelectSingleNode("NETWORKSET");
                root = xmlDoc.SelectSingleNode("NETWORKSET");//查找<NETWORKSET>
                XmlElement xe1 = xmlDoc.CreateElement("SET3");//创建一个<book>节点

                xe1.SetAttribute("NAME", (nodeList.Count + 1).ToString());//设置该节点属性及属性值
                xe1.SetAttribute("TEXT", this.textBox1.Text.Trim());//设置该节点属性及属性值

                XmlElement xesub1 = xmlDoc.CreateElement("AUTO_IP");
                xesub1.InnerText = "0";//设置文本节点
                xe1.AppendChild(xesub1);//添加到<book>节点中

                XmlElement xesub2 = xmlDoc.CreateElement("IP_ADDRESS");
                xesub2.InnerText = "";//设置文本节点
                xe1.AppendChild(xesub2);//添加到<book>节点中

                XmlElement xesub3 = xmlDoc.CreateElement("IP_ZWYM");
                xesub3.InnerText = "";//设置文本节点
                xe1.AppendChild(xesub3);//添加到<book>节点中

                XmlElement xesub4 = xmlDoc.CreateElement("IP_MRWG");
                xesub4.InnerText = "";//设置文本节点
                xe1.AppendChild(xesub4);//添加到<book>节点中

                XmlElement xesub5 = xmlDoc.CreateElement("AUTO_DNS");
                xesub5.InnerText = "0";//设置文本节点
                xe1.AppendChild(xesub5);//添加到<book>节点中

                XmlElement xesub6 = xmlDoc.CreateElement("DNS_SX");
                xesub6.InnerText = "";//设置文本节点
                xe1.AppendChild(xesub6);//添加到<book>节点中

                XmlElement xesub7 = xmlDoc.CreateElement("DNS_BX");
                xesub7.InnerText = "";//设置文本节点
                xe1.AppendChild(xesub7);//添加到<book>节点中

                root.AppendChild(xe1);//添加到<bookstore>节点中
                xmlDoc.Save("NETWORKSET.xml");


                //by.BindListBox(nodeList.Count - 1);
                //(ParentForm as Body).BindListBox(nodeList.Count);

                this.Close();
            }
        }
    }
}
